package com.bestv.search.spider.douban.util;

import com.bestv.search.spider.douban.model.DouBanAppKey;
import com.bestv.search.spider.util.SpiderConst;
import com.bestv.search.spider.util.SpiderUtil;
import com.bestv.search.spider.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DouBanUtil extends SpiderUtil {

    private final Log log = LogFactory.getLog(DouBanUtil.class);

    public String sendRequestByAppKey(String url) {
        DouBanAppKey key = DouBanAppKeyPool.getAppKey();
        if (key == null) {
            log.warn("API key is busy, please wait....");
            return null;
        } else {
            StringBuffer buffer = null;
            if (url.endsWith(SpiderConst.QUESTION_MARK)) {
                buffer = StringUtil.buildBuffer(new String[]{url, SpiderConst.DOUBAN_DPIKEY, SpiderConst.EQUAL_FLAG,
                        key.getKeyId()});
            } else {
                buffer = StringUtil.buildBuffer(new String[]{url, SpiderConst.URL_SPLIT_FLAG,
                        SpiderConst.DOUBAN_DPIKEY, SpiderConst.EQUAL_FLAG, key.getKeyId()});
            }
            return sendRequest(buffer.toString());
        }
    }
}
